package com.glaive;

import com.google.common.collect.Maps;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.glaive.utils.AddressUtils.AddressList;

public class TxtIndexHandleCmd20231125 {
    static String filePath = System.getProperty("user.dir") + "\\茅山捉鬼人20231005.txt";

    public static void main(String[] args) throws Exception {
        readLine();
    }

    public static String defaultIndexString = "章";
    public static Long indexCount = 1L;


    /**
     *
     */
    public static void readLine() throws Exception {
        //正则说明：
        Pattern pattern = Pattern.compile(
                "((\\d{1,5}.*?)?((第(.*?)" + defaultIndexString + ")(.*)))"
        );

        Files
                .lines(Paths.get(filePath))
                .map(s -> {
                    Matcher m = pattern.matcher(s);
                    for (String k : AddressList) {
                        String[] split = k.split("——");
                        s = s.replace(split[1], split[0]);
                    }
                    if (m.find()) {
                        String group = m.group(1);
                        String finalName = "";
                        if (isValue(group)) {
                            return s;
                        } else {
//                            System.out.print(indexCount + "=");
                            String g6 = m.group(6).trim();
                            String[] s1 = g6.split(" ");
                            boolean hasC = s1.length > 1;
                            if (hasC) {
                                finalName = s1[0] + "章 " + s1[1];
                            } else {
                                finalName = m.group(4) + " " + g6;
                            }
                            //调试时使用
//                            System.out.print(
//                                    m.group(1) +
//                                            " | " +
//                                            m.group(2) +
//                                            " | " +
//                                            m.group(3) +
//                                            " | " +
//                                            m.group(4) +
//                                            " | " +
//                                            m.group(5) +
//                                            " | " +
//                                            g6
//                            );
//                            System.out.println(".  finalName:" + finalName);
                        }
                        indexCount++;
//                        System.out.println(m.group(4) + "," + m.group(5) + "," + m.group(6));
//                        int xx = ChineseNum2int.chineseNumToArabicNum(m.group(2));
//                        String volume = "第" + xx + defaultIndexString;
//                        String indexSection = m.group(1);
//                        String index = volume + " " + indexSection;
//                        System.out.println(index);
                        return finalName;
//                        return "";
                    } else
                        return s;
//                        return "";

                })
//                .distinct()
                .forEach(s -> {
                    System.out.println(s);
                });
    }

    static List<String> valueList = Arrays.asList(
            "第一章，就是体术",
            "第二天一早，苏钦章就来了",
            "第一弟子，你跟苏钦章他们有什么办法阻止啊",
            "第二天，叶少阳就在小别墅里呆着",
            "第一章第一条，任何时候都绝不丢下一个成员",
            "第二部于今晚开更",
            "第一部分卷目录一份，见相关篇章",
            "第一个十几个座位，是真皮沙发的",
            "第五遍，祁宸终于走出了村子",
            "第一智者！”苏钦章钦佩地举起大拇指",
            "第三代弟子，陈平和张小蕊的儿子",
            "第一天才，当初他冲击地仙的最后关头",
            "第342X章 关于地名的修改",
            "第二，他是玄清山弟子",
            "第一章项小羽的"
    );

    private static boolean isValue(String group) {
        for (String v : valueList) {
            boolean contains1 = group.contains(v);
            if (contains1) {
                return true;
            }
        }
        return false;
    }

    static String volumeSection = "";

    /**
     * 索引 去重
     *
     * @param volume
     * @param section
     * @return
     */
    private static boolean indexIsRepeat(String volume, String section) {
        if (volumeSection.equals(volume + section)) {
            return true;
        } else {
            volumeSection = volume + section;
            return false;
        }

    }

    static String volumeTmp = "";

    /**
     * 卷的索引处理：只有第一卷的首次出现 都会显示卷的标题，其他省略
     */
    private static String volumeHandle(String volume, String volumeValue) {
        if (!volumeTmp.equals(volume + volumeValue)) {
            volumeTmp = volume + volumeValue;
            return volumeTmp;
        } else {
            return volume;
        }
    }
}